/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2023-2023. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 and
 * only version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * Description:
 * Author: huawei
 * Create: 2023-08-21
 */

#include "svm_phy_addr_blk_mng.h"
#include "svm_vmma_mng.h"
#include "svm_mem_map.h"

void devmm_mem_unmap(struct devmm_svm_process *svm_proc, struct devmm_vmma_info *info)
{
    struct devmm_phy_addr_blk *blk = NULL;

    blk = devmm_phy_addr_blk_get(&svm_proc->phy_addr_blk_mng, info->phy_addr_blk_id);
    if (unlikely(blk == NULL)) {
        devmm_drv_warn("Get phy_addr_blk failed. (id=%d)\n", info->phy_addr_blk_id);
        return;
    }

    devmm_zap_pages(svm_proc, info->va, info->pg_num, info->pg_type);
    devmm_phy_addr_blk_occupy_dec(blk);
    devmm_phy_addr_blk_put(blk);
}

static int _devmm_mem_map(struct devmm_svm_process *svm_proc,
    struct devmm_phy_addr_blk *blk, struct devmm_vmma_info *info)
{
    struct page **pages = NULL;

    pages = devmm_phy_addr_blk_get_pages(blk, info->offset_pg_num, info->pg_num);
    if (pages == NULL) {
        return -ENOMEM;
    }

    return devmm_remap_pages(svm_proc, info->va, pages, info->pg_num, info->pg_type);
}

static int devmm_mem_map_info_check(struct devmm_phy_addr_blk *blk, struct devmm_vmma_info *info)
{
    /* check va's pg_type */
    if (info->pg_type != blk->attr.pg_type) {
        devmm_drv_err("Va's pg_type doesn't match with pa's pg_type. (va_pg_type=%u; pa_pg_type=%u)\n",
            info->pg_type, blk->attr.pg_type);
        return -EINVAL;
    }

    /* check handle's pg_num */
    if (info->phy_addr_blk_pg_num != blk->pg_num) {
        devmm_drv_err("Handle's pg_num is not match. (handle->pg_num=%llu; blk->pg_num=%llu)\n",
            info->phy_addr_blk_pg_num, blk->pg_num);
        return -EINVAL;
    }

    /* check handle's module_id */
    if (info->module_id != blk->attr.module_id) {
        devmm_drv_err("Handle's module_id is not match. (handle->module_id=%u; blk->module_id=%u)\n",
            info->module_id, blk->attr.module_id);
        return -EINVAL;
    }

    return 0;
}

int devmm_mem_map(struct devmm_svm_process *svm_proc, struct devmm_vmma_info *info)
{
    struct devmm_phy_addr_blk *blk = NULL;
    int ret = -EINVAL;

    blk = devmm_phy_addr_blk_get(&svm_proc->phy_addr_blk_mng, info->phy_addr_blk_id);
    if (blk == NULL) {
        devmm_drv_err("Get phy_addr_blk failed. (id=%d)\n", info->phy_addr_blk_id);
        return -EINVAL;
    }

    ret = devmm_mem_map_info_check(blk, info);
    if (ret != 0) {
        devmm_phy_addr_blk_put(blk);
        return ret;
    }

    ret = devmm_phy_addr_blk_occupy_inc(blk);
    if (unlikely(ret != 0)) {
        devmm_phy_addr_blk_put(blk);
        return ret;
    }

    ret = _devmm_mem_map(svm_proc, blk, info);
    if (ret != 0) {
        devmm_phy_addr_blk_occupy_dec(blk);
    }

    devmm_phy_addr_blk_put(blk);
    return ret;
}
